package com.example.onlinemusic.controller;

import com.example.onlinemusic.model.Music;
import com.example.onlinemusic.model.User;
import com.example.onlinemusic.service.LoveMusicService;
import com.example.onlinemusic.tools.Constant;
import com.example.onlinemusic.tools.ResponseBodyMessage;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/lovemusic")
public class LoveMusicController {
    @Resource
    private LoveMusicService loveMusicService;

    @RequestMapping("/likemusic")
    public ResponseBodyMessage<Boolean> likeMusic(@RequestParam String id, HttpServletRequest request){
        int musicId=Integer.parseInt(id);
        System.out.println("music:"+musicId);
        //检查是否登录了
        HttpSession httpSession = request.getSession(false);
        if(httpSession==null||httpSession.getAttribute(Constant.USERINFO_SESSION_KEY)==null){
            return new ResponseBodyMessage<>(-1,"请登陆后上传",false);
        }
        User user=(User)httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId= user.getId();
        System.out.println("userId:"+userId);

        Music music=loveMusicService.findLoveMusicByMusicIdAndUserId(userId,musicId);

        if(music!=null){
            //不能重复收藏要取消收藏
            return new ResponseBodyMessage<>(-1,"你之前收藏过这个音乐",false);
        }
        boolean effect =loveMusicService.insertLoveMusic(userId,musicId);
        if(effect){
            return new ResponseBodyMessage<>(0,"收藏成功",true);
        }else{
            return new ResponseBodyMessage<>(-1,"收藏失败",false);
        }
    }

    @RequestMapping("/findlovemusic")
    public ResponseBodyMessage<List<Music>> findLoveMusic(@RequestParam(required = false) String musicName,
                                        HttpServletRequest request){
        HttpSession httpSession = request.getSession(false);
        if(httpSession==null||httpSession.getAttribute(Constant.USERINFO_SESSION_KEY)==null){
            return new ResponseBodyMessage<>(-1,"请登陆后上传",null);
        }
        User user=(User)httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId= user.getId();
        System.out.println("userId:"+userId);

        List<Music> musicList=null;
        if(musicName==null){
            musicList=loveMusicService.findLoveMusicByUserId(userId);
        }else{
            musicList=loveMusicService.findLoveMusicBykeyAndUID(musicName,userId);
        }

        return new ResponseBodyMessage<>(0,"查询到了所有的歌曲信息",musicList);
    }

    @RequestMapping("/deletelovemusic")
    public ResponseBodyMessage<Boolean> deletelovemusic(HttpServletRequest request,
                                                        @RequestParam String id){
        int musicId=Integer.parseInt(id);

        HttpSession httpSession=request.getSession(false);
        if(httpSession==null||httpSession.getAttribute(Constant.USERINFO_SESSION_KEY)==null){
            return new ResponseBodyMessage<>(-1,"请登录后上传",null);
        }
        User user=(User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId= user.getId();
        System.out.println("userId"+userId);

        int ret=loveMusicService.deleteLoveMusic(userId,musicId);

        if(ret==1){
            return new ResponseBodyMessage<>(0,"取消收藏音乐成功",true);
        }else{
            return new ResponseBodyMessage<>(-1,"取消收藏音乐失败",false);
        }
    }
}
